package com.scopionstudio.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


public class JsonParser {
public JsonParser(){
		
	}
	public static String getJSONString(String url) {
	    String jsonString = null;
	    HttpURLConnection linkConnection = null;
	    try {
	        URL linkurl = new URL(url);
	        linkConnection = (HttpURLConnection) linkurl.openConnection();
	        int responseCode = linkConnection.getResponseCode();
	        if (responseCode == HttpURLConnection.HTTP_OK) {
	            InputStream linkinStream = linkConnection.getInputStream();
	            ByteArrayOutputStream baos = new ByteArrayOutputStream();
	            int j = 0;
	            while ((j = linkinStream.read()) != -1) {
	                baos.write(j);
	            }
	            byte[] data = baos.toByteArray();
	            jsonString = new String(data);
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        if (linkConnection != null) {
	            linkConnection.disconnect();
	        }
	    }
	    return jsonString;
	}

	public static boolean isNetworkAvailable(Activity activity) {
		ConnectivityManager connectivity = (ConnectivityManager) activity
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
